import add from './sportsman-add.js'
import del from './sportsman-del.js'
import update from './sportsman-update.js'

let datagrid = document.querySelector("._datagrid");
let gender = ["女","男"];
console.debug(datagrid);

let loadData = function() {
	fetch("Sportsman-list.let").then((r) => r.json()).then((data) => {
		console.debug(data);
		datagrid.innerHTML = "";
		data.forEach((item, index) => {
			let tr = document.createElement("tr");
			let year = new Date(item.sportsmanBrithdate).getFullYear();
			let mounth = new Date(item.sportsmanBrithdate).getMonth() + 1;
			let date = new Date(item.sportsmanBrithdate).getDate();
			let dd = year + "/" + mounth + "/" + date;
			console.debug(dd);
			tr.innerHTML = `
				<td style="padding:3px; width:50px"><img style="width:50px; height:63px; margin-left:5px;" src="../demo/down.let?file=${item.sportsmanUri}"></td>
				<td>${item.countryName}</td>
				<td>${item.sportsmanName}</td>
				<td>${gender[item.sportsmanGender]}</td>
				<td>${dd}</td>
				<td>${item.sportsmanHeight}</td>
				<td>${item.sportsmanWeigth}</td>
				<td class="p-2">
					<div class="btn-group btn-group-sm">
						<button type="button" class="btn btn-primary _update" title="修改">
							<i class="fa fa-edit"></i>
						</button>
						<button type="button" class="btn btn-danger _delete" title="删除">
							<i class="fa fa-remove"></i>
						</button>
					</div>
				</td>`;
			datagrid.appendChild(tr);
			tr.querySelector("._update").onclick = function() {
				console.debug("info:"+item);
				update(item);
			}
			tr.querySelector("._delete").onclick = function() {
				del(item);
			}
		});
	}).catch((e) => {
		console.error(e);
	});
}
loadData();
window.addEventListener("message", function(e) {
	console.debug(e);
	if (e.data == "loadData") {
		loadData();
	}
});
add();